package ch.unizh.ifi.ailab.camera;

/**
 * @author jonas boesch
 * edited and enhanced by hans jaeckle
 *
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import ch.unizh.ifi.ailab.config.ProgramSettings;
import ch.unizh.ifi.ailab.utils.Utils;



public class ImagePanel extends JPanel {
	private String fileSeparator = System.getProperty("file.separator");

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// properties
	private ProgramSettings myProp = ProgramSettings.getProgramSettings();

	// logging
	private Logger myLog = Logger.getLogger(ImagePanel.class);

	Image mImage; // , image2;

	public ImagePanel() {
		myLog.debug("kamerabild: initialisiere imagepanel.");
		String stdImage = Utils.path(System.getProperty("user.dir"), "conf"
				+ fileSeparator + myProp.getProperty("placeholderImage"));
		try {
			myLog.debug("kamerabild: versuche bild zu laden: "
					+ stdImage.toString());
			mImage = ImageIO.read(new File(stdImage));
		} catch (MalformedURLException e) {
			myLog.warn("fehler - falsch formatierte url - " + e.toString());
		} catch (IOException e) {
			myLog.warn("fehler - io fehler - " + e.toString());
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(mImage, 0, 0, this);
	}

	/**
	 * public void updateImage() { mImage = myCam.getActualImage(); repaint(); }
	 */
	public void updateImage(Image image) {
		mImage = image;
		repaint();
	}

	// return size of larger image for scrollpane
	public Dimension getPreferredSize() {
		return new Dimension(mImage.getWidth(this), mImage.getHeight(this));
	}
}
